# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
from itemadapter import ItemAdapter
import pymysql


class DoubanproPipeline:
    fp = None

    # 重写父类的方法，只能被调用一次
    def open_spider(self, spider):
        print("开始爬虫。。。。")
        self.fp = open('./douban.txt', mode="w", encoding="utf-8")

    # 专门用来处理items类型的对象
    # 该方法可以处理爬虫文件提交过来的item对象
    # 该方法每接受一个item就会被调用一次
    def process_item(self, item, spider):
        title = item['title']
        address = item['address']
        print(item['title'], item['address'])
        self.fp.write(title + ":" + address + "\n")
        return item  # 会将item传递给下一个即将被指定的管道类

    def close_spider(self, spider):
        print("结束爬虫")
        self.fp.close()


# 管道文件中的一个管道类对应将一组数据存储到一个平台或者载体中
# 这个管道类 会将数据持久化到数据库
class mysqlPipleline:
    conn = None
    course = None

    def open_spider(self, spider):
        print("开始爬虫。。。。")
        self.conn = pymysql.connect(host="localhost", user="root", password="123456", port=3306, db="web",
                                    charset="utf8")

    def process_item(self, item, spider):
        self.cursor = self.conn.cursor()
        try:
            self.cursor.execute(
                "insert into douban (id,title, address) values (null,'%s', '%s')" % (item['title'], item['address']))
            self.conn.commit()
        except Exception as e:
            print(e)
            self.conn.rollback()
        return item

    def close_spider(self, spider):
        self.cursor.close()
        self.conn.close()
